﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using BackupConsoleClient.TagService;

namespace BackupConsoleClient
{
    /// <summary>
    /// The actions supported for tags.
    /// </summary>
   public static class TagCommandActions
    {
        /// <summary>
        /// The instance of the tag client service.
        /// </summary>
        private static readonly TagService.TagWebServiceClient TagWebServiceClient;

        static TagCommandActions()
        {
            TagWebServiceClient = new TagWebServiceClient();
        }


        /// <summary>
        /// Save a specific tag.
        /// </summary>
        public static void Save()
        {
            var tagEntity = new TagEntity()
            {
                Name = CommandLineArguments.Instance.Name
            };

            if (!string.IsNullOrWhiteSpace(CommandLineArguments.Instance.ParentTagId))
            {
                Guid parentTagId;
                if (!Guid.TryParse(CommandLineArguments.Instance.ParentTagId, out parentTagId))
                {
                    throw new ArgumentException("The parent tag id is not valid.");
                }

                tagEntity.Parent = Guid.Parse(CommandLineArguments.Instance.ParentTagId);
            }

            TagWebServiceClient.Save(
                tagEntity,
                new TagService.Credentials()
                {
                    User = CommandLineArguments.Instance.User,
                    Password = CommandLineArguments.Instance.Password
                });
        }

        /// <summary>
        /// Delete a specific tag
        /// </summary>
        public static void Delete()
        {
            TagWebServiceClient.Delete(
                Guid.Parse(CommandLineArguments.Instance.Id),
                new TagService.Credentials()
                {
                    User = CommandLineArguments.Instance.User,
                    Password = CommandLineArguments.Instance.Password
                });
        }

        /// <summary>
        /// Get the parent tag.
        /// </summary>
        /// <returns></returns>
        public static TagEntity GetParent()
        {
            return TagWebServiceClient.GetParent(
                  new TagEntity()
                  {
                      Id = Guid.Parse(CommandLineArguments.Instance.Id)
                  },
                      new TagService.Credentials()
                      {
                          User = CommandLineArguments.Instance.User,
                          Password = CommandLineArguments.Instance.Password
                      });
        }

        /// <summary>
        /// Get childrens of the given tag.
        /// </summary>
        /// <returns>The list of tags.</returns>
        public static ReadOnlyCollection<TagEntity> GetChildrens()
        {
            return TagWebServiceClient.GetChildrens(
                  new TagEntity()
                  {
                      Id = Guid.Parse(CommandLineArguments.Instance.Id)
                  },
                      new TagService.Credentials()
                      {
                          User = CommandLineArguments.Instance.User,
                          Password = CommandLineArguments.Instance.Password
                      }).ToList().AsReadOnly();
        }

        /// <summary>
        /// Load the tag with tihe given id.
        /// </summary>
        /// <returns>The tag with the given id.</returns>
        public static TagEntity Load()
        {
            return TagWebServiceClient.Load(
                        Guid.Parse(CommandLineArguments.Instance.Id),
                        new TagService.Credentials()
                        {
                            User = CommandLineArguments.Instance.User,
                            Password = CommandLineArguments.Instance.Password
                        });
        }

        /// <summary>
        /// Load all tags from the repository.
        /// </summary>
        /// <returns>The list of tags.</returns>
        public static ReadOnlyCollection<TagEntity> LoadAll()
        {
            return TagWebServiceClient.LoadAll(new TagService.Credentials()
            {
                User = CommandLineArguments.Instance.User,
                Password = CommandLineArguments.Instance.Password
            }).ToList().AsReadOnly();
        }
    }
}
